﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ResourceTranslator
{
    /// <summary>
    /// Interaction logic for LabeledControl.xaml
    /// </summary>
    public partial class LabeledControl : StackPanel
    {
        public string Label
        {
            get { return (string)GetValue(LabelProperty); }
            set { SetValue(LabelProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Label.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty LabelProperty =
            DependencyProperty.Register("Label", typeof(string), typeof(LabeledControl), new UIPropertyMetadata("Label:"));


        UIElement control;
        public UIElement Control
        {
            get
            {
                return control;
            }
            set
            {
                if (control != null)
                {
                    this.Children.Remove(control);
                }
                control = value;

                if (this.Children.Count > 0)
                    this.Children.Insert(1, control);
                else
                    this.Children.Add(control);
            }
        }

        protected override void OnInitialized(EventArgs e)
        {
            TextBlock tb = new TextBlock();
            this.Children.Insert(0, tb);
            tb.VerticalAlignment = VerticalAlignment.Center;
            Binding labelBinding = new Binding("Label");
            labelBinding.Source = this;
            BindingOperations.SetBinding(tb, TextBlock.TextProperty, labelBinding);

            base.OnInitialized(e);
        }
    }
}
